var prefix = "/api/grade/student";
var myGrid;

//批量删除按钮
function batchRemove() {
    myGrid.batchRemove();
}

//显示增加对话框按钮
function showAddModel() {
    myGrid.showAddModel();
}

//显示更新对话框按钮
function showUpdateModel(id) {
    myGrid.showUpdateModel(id);
}

//导出Excel按钮
function outExcel(value) {
    myGrid.exportExcel(value);
}

//查詢按钮
function searchTable() {
    myGrid.loadTable();
}

//毕业设计下拉列表
function initgrdesgin(){

    Tms.select2AjaxData("/api/grade/dimclass/getAll",
        "#srclass_id", "id", "className");

}


function initWebUploader(){

    // 初始化Web Uploader
    var uploader = WebUploader.create({

        // 选完文件后，是否自动上传。
        auto: true,

        // 禁掉分块传输，默认是开起的。
        chunked: false,
        server: '/api/grade/student/importExcel', //后台接收服务

        pick: {
            id: '#filePicker',
            multiple: false
        },

        // 只允许选择Excel文件。
        accept: {
            title: 'Excel',
            extensions: 'xls,xlsx',
            mimeTypes: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        }
    });



    //上传成功
    uploader.on( "uploadSuccess", function( file,rs ) {
        console.log("file upload success! "+file);
        if(rs.code ==1){
            //sucess
            setTimeout(10000,function () {

                myGrid.reLoadTable();
            })
            // $("#avatarImg").attr("src",rs.data);
            //
            // $("#avatar").val(rs.data);
        }
        // $( "#"+file.id ).find("p.state").text("已上传");
        // $('#' + file.id).find('.progress').fadeOut();
    });

    //上传失败
    uploader.on( "uploadError", function( file ) {
        // $( "#"+file.id ).find("p.state").text("上传出错");
        // uploader.cancelFile(file);
        // uploader.removeFile(file,true);
    });


}


$(function () {
    var options = {
        ctrfix: "/grade/student",//当前控制器
        recordId: 'id',
        tableId: '#student_Table',
        AddDlgTitle: '新增',
        updateDlgTitle: '更新',
        recordId:'id',
        sort: 'id',
        direct: 'desc',
        deleteURL: function (id) {
            return prefix + "/batchRemove";
        },
        listURL: function () {
            return prefix + "/getByPage";
        },
        outExcel:function(){
            return prefix + "/exportExcel";
        },

        columns: [
            {
                checkbox: true
            },
            {
                field: 'id',
                title: '序号',
            },
            {
                field: 'className',
                title: ' 班级名',
            },
            {
                field: 'username',
                title: '学号',
            },
            {
                field: 'name',
                title: '姓名',
            },
            {
                field: 'gender',
                title: '性别',
                formatter: function (data, type, row, meta) {
                    return "<span>" + Tms.formatSex(data) + "</span>";
                }
            },
            {
                field: 'avatar',
                title: '头像',
                formatter: function (data, type, row, meta) {
                    if(data)
                        return "<img  class='avatar' src='" + data + "' />";
                    else
                        return  "";
                }
            },
            {
                data : null,
                title: '操作',
                width: '50px',
                formatter: function (data,  row, meta) {
                    var operators = '<a type="button"  href="#" class="btn btn-mint btn-xs btn-icon" onclick=showUpdateModel(' + row.id + ',"' + options.updateDlgTitle + '") title="' + options.updateDlgTitle + '"><i class="demo-psi-pen-5 icon-lg"></i></a>';
                    return operators;
                }
            }
        ]
    }

    if (s_edit_h == '') {
        options.editable = true;
    } else {
        options.editable = false;
    }

    myGrid = new gridViewModel(options);
    myGrid.init();
    initgrdesgin();
    initWebUploader();

})